nyoj 349 Sorting It All Out(拓扑排序度的理解)

Sorting It All Out

时间限制: 3000 ms  |  内存限制: 65535 KB
难度: 3
描述
An ascending sorted sequence of distinct values is one in which some form of a less-than operator is used to order the elements from smallest to largest. For example, the sorted sequence A, B, C, D implies that A < B, B < C and C < D. in this problem, we will give you a set of relations of the form A < B and ask you to determine whether a sorted order has been specified or not.
输入
Input consists of multiple problem instances. Each instance starts with a line containing two positive integers n and m. the first value indicated the number of objects to sort, where 2 <= n <= 26. The objects to be sorted will be the first n characters of the uppercase alphabet. The second value m indicates the number of relations of the form A < B which will be given in this problem instance. Next will be m lines, each containing one such relation consisting of three characters: an uppercase letter, the character "<" and a second uppercase letter. No letter will be outside the range of the first n letters of the alphabet. Values of n = m = 0 indicate end of input.
输出
For each problem instance, output consists of one line. This line should be one of the following three: 

Sorted sequence determined after xxx relations: yyy...y. 
Sorted sequence cannot be determined. 
Inconsistency found after xxx relations. 

where xxx is the number of relations processed at the time either a sorted sequence is determined or an inconsistency is found, whichever comes first, and yyy...y is the sorted, ascending sequence. 

样例输入
4 6
A<B
A<C
B<C
C<D
B<D
A<B
3 2
A<B
B<A
26 1
A<Z
0 0

样例输出
Sorted sequence determined after 4 relations: ABCD.
Inconsistency found after 2 relations.
Sorted sequence cannot be determined.
来源
POJ
上传者

陈玉

输入n和m,n表示26个字母前n个字母,m表示有多少个关系,然后输入m个关系,判断是否这n个字母存在一个排序关系
如果存在输出在几个关系之后就输出几个关系之后就可以确定,比如第一个测试数据,前四个关系输入之后,就输出结果
后两个关系输入不用管,,如果存在环那么就输出冲突,如果不能确定次序就输出不能确定。
拓扑排序的理解,度的判断 

#include<stdio.h>
#include<string.h>
#define M 30
int re[M],path[M][M],du[M],d[M];
char str[8];
int topo(char s[],int n){
	int i,j,k;
	int A=s[0]-'A';
	int B=s[2]-'A';
	if(path[A][B]==0){
		path[A][B]=1;
		d[B]++;//因为每一次调用函数的时候要保持原来的图的数据,所以用d数组保存度不变 
	}
	for(i=0;i<n;i++)
		du[i]=d[i];//du数组来判断当前函数度的变化 
	int flag=1,cnt,pos=0;
	for(i=0;i<n;i++){
		cnt=0;
		for(j=0;j<n;j++){
			if(du[j]==0){
				cnt++;
				k=j;
			}
		}
		if(cnt==0) return -1;//cnt等于0表示存在环,即存在冲突 
		else if(cnt>1)	flag=0;//存在多个度为0的点,即次序不能确定 
		du[k]--;
		re[pos++]=k;
		for(j=0;j<n;j++){
			if(path[k][j]==1)
				du[j]--;
		}
	}
	if(flag) return 1;
	return 0; 
}
void result(int n,int m){
	int i,j;
	int ok=1;
	for(i=0;i<m;i++){
		scanf("%s",str);
		if(ok){
			int t=topo(str,n);
			if(t==1){
				printf("Sorted sequence determined after %d relations: ",i+1);
				for(j=0;j<n;j++)
					printf("%c",char(re[j]+'A'));
				printf(".\n");
				ok=0;
			}
			else if(t==-1){
				printf("Inconsistency found after %d relations.\n",i+1);
				ok=0;
			}
		}
	}
	if(ok)	printf("Sorted sequence cannot be determined.\n");
}
int  main(){
	int n,m;
	char str[8];
	while(scanf("%d%d",&n,&m),n+m){
		if(m<n-1){//关系不够不能确定次序 
			for(int i=0;i<m;i++)
				scanf("%s",str);
			printf("Sorted sequence cannot be determined.\n");
		}else{
			memset(d,0,sizeof(d));
			memset(path,0,sizeof(path));
			result(n,m);
		}
	}
	return 0;
}


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
x1y2 x2y3 x3y1-x1y3-x2y1-x3y2 是计算三角形面积的公式中的一部分。 在这个公式中,x1、x2、x3分别表示三角形的三个顶点的x坐标,y1、y2、y3分别表示三角形的三个顶点的y坐标。通过计算这个表达式的值,可以得到三角形的面积。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [TetraCluster:使用并行Java 2库的Java并行程序。 该程序在群集并行计算机上运行,​​以从给定的点集中找到...](https://download.csdn.net/download/weixin_42171208/18283141)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [线性代数有个题,求正交变换x=Qy,化二次型f(x1,x2,x3)=8x1x2+8x1x3+8x2x3为标准型求出特征值](https://blog.csdn.net/weixin_39956182/article/details/115882118)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [nyoj-67-三角形面积(S=(1/2)*(x1y2+x2y3+x3y1-x1y3-x2y1-x3y2))](https://blog.csdn.net/weixin_30492601/article/details/99541033)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值